package com.itqf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.entity.ScheduleJob;
import com.itqf.entity.ScheduleJobExample;
import com.itqf.mapper.ScheduleJobMapper;
import com.itqf.service.SchedulerService;
import com.itqf.utils.*;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 徐老板
 * @date 2020/1/31   9:52
 */
@Service
public class SchedulerServiceImpl implements SchedulerService {

    @Resource
    ScheduleJobMapper scheduleJobMapper;

    @Override
    public ResultData schedulerList(Pager pager, String search) {
        PageHelper.offsetPage(pager.getOffset(),pager.getLimit() );
        //后台校验指对传过来的参数进行校验
        ScheduleJobExample scheduleJobExample=new ScheduleJobExample();
        if (StringUtils.notEmpty(search)){

            ScheduleJobExample.Criteria criteria = scheduleJobExample.createCriteria();
            criteria.andBeanNameLike("%"+search+"%");
        }
//这块自己写还是不熟练
        List<ScheduleJob> scheduleJobList = scheduleJobMapper.selectByExample(scheduleJobExample);
        PageInfo pageInfo=new PageInfo(scheduleJobList);
        ResultData resultData=new ResultData(pageInfo.getTotal(),pageInfo.getList() );

        return resultData;
    }
    //执行schedulejob的增删改查

    @Resource
    Scheduler scheduler;


    @Override
    public R save(ScheduleJob scheduleJob) throws SchedulerException {



        int insert = scheduleJobMapper.insert(scheduleJob);
        scheduler.start();
        ScheduleUtils.createScheduleTask(scheduler );

        return insert>0?R.ok():R.error();
//        return null;
    }

    @Override
    public R delete(List<Long> jobIds) {
        ScheduleJobExample scheduleJobExample=new ScheduleJobExample();
        ScheduleJobExample.Criteria criteria = scheduleJobExample.createCriteria();
        criteria.andJobIdIn(jobIds);
        int delete = scheduleJobMapper.deleteByExample(scheduleJobExample);
        return delete>0?R.ok():R.error();
    }

    @Override
    public R update(ScheduleJob scheduleJob) {
        int insert = scheduleJobMapper.updateByPrimaryKeySelective(scheduleJob);

        return insert>0?R.ok():R.error();
    }

    @Override
    public R get(Long jobId) {
        ScheduleJob scheduleJob = scheduleJobMapper.selectByPrimaryKey(jobId);

        return R.ok().put("scheduleJob", scheduleJob);
    }

    @Override
    public R pause(List<Long> jobIds) {
        ScheduleJobExample scheduleJobExample=new ScheduleJobExample();
        ScheduleJobExample.Criteria criteria = scheduleJobExample.createCriteria();
        criteria.andJobIdIn(jobIds);
        ScheduleJob scheduleJob =new ScheduleJob();
        scheduleJob.setStatus(SysConstant.ScheduleState.PAUSE.getValue());
        int delete = scheduleJobMapper.updateByExampleSelective(scheduleJob,scheduleJobExample);
         return delete>0?R.ok():R.error();


    }

    @Override
    public R resume(List<Long> jobIds) {
        ScheduleJobExample scheduleJobExample=new ScheduleJobExample();
        ScheduleJobExample.Criteria criteria = scheduleJobExample.createCriteria();
        criteria.andJobIdIn(jobIds);
        ScheduleJob scheduleJob =new ScheduleJob();
        scheduleJob.setStatus(SysConstant.ScheduleState.NOMAL.getValue());
        int delete = scheduleJobMapper.updateByExampleSelective(scheduleJob,scheduleJobExample);
        return delete>0?R.ok():R.error();
    }

    @Override
    public R run(List<Long> jobIds) {
        return R.ok();
    }


}
